//tuple元组: 多种元素的组合

//1.数组
//数组中的数据类型应该保持统一
// let arr: any[] = ["name", 18, true]
// arr[0].length;
// arr[1].length;
//如果不统一,那么应该尝试使用对象替代
/*let obj = {
    name: "name",
    age: 18,
    n: true
}*/


//2.元组 必须要赋值
let arr: [string, number, boolean] = ["name", 18, true]
arr[0].length;
// arr[1].length;


//应用场景
function createState(state: any) {
    let curState = state;
    let setState = (newValue: any) => {
        curState = newValue
    }
    let arr: [any, (newValue: any) =>void] = [curState, setState]
    return arr
}

let [curState, setState] = createState(100);
setState(101);
console.log(curState)


let [title, setTitle] = createState("abc")
setTitle("aaa")
console.log(title)

//优化
function createState2<T>(state: T) {
    let curState = state;
    let setState = (newValue: T) => {
        curState = newValue
    }
    let arr: [T, (newValue: T) =>void] = [curState, setState]
    return arr
}

let [flag, setFlag] = createState(true)
setTitle(false)
console.log(title)

export {}